package com.henry.chatkit.weight.cellview

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SizeUtils
import com.henry.chatkit.R
import com.henry.chatkit.glide.loadImage
import com.henry.chatkit.weight.badgeview.BadgeView


@SuppressLint("Recycle", "ResourceAsColor")
class UITableViewCell @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : RelativeLayout(context, attrs, defStyleAttr) {

    val ACCESSORY_TYPE_DEFAULT = 0 // 附件类型 默认没有

    val ACCESSORY_TYPE_ARROW = 1 // 右箭头

    val ACCESSORY_TYPE_BADGE = 2 // badge

    val ACCESSORY_TYPE_SWITCH = 3 // switch开关

    val ACCESSORY_TYPE_BUTTON = 4 // 按钮 默认删除按钮

    var imageView: ImageView? = null
    var titleLabel: TextView? = null
    var detailLabel: TextView? = null
    var accessoryLayout: RelativeLayout? = null
    var lineView: View? = null

    var badgeView: BadgeView? = null

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    var switch: Switch? = null
    var button: Button? = null

    var image: Any?
        get() {
            return null
        }
        set(value) {
            if (value == null) {
                imageView?.visibility = GONE
            } else {
                when (value) {
                    is Int -> {
                        if (value == 0) {
                            imageView?.visibility = GONE
                        } else {
                            imageView?.visibility = VISIBLE
                            imageView?.setImageResource(value)
                        }
                    }
                    is String -> {
                        if (value.isEmpty()) {
                            imageView?.visibility = GONE
                        } else {
                            imageView?.visibility = VISIBLE
                            imageView?.loadImage(value)
                        }
                    }
                    is Drawable -> {
                        imageView?.visibility = VISIBLE
                        imageView?.setImageDrawable(value)
                    }
                }
            }
        }

    var title: String?
        get() {
            return null
        }
        set(value) {
            titleLabel?.text = value?:""
        }
    var titleColor: Int
        get() {
            return 0
        }
        set(value) {
            titleLabel?.setTextColor(value)
        }
    private var titleSize: Float
        get() {
            return 0F
        }
        set(value) {
            titleLabel?.setTextSize(TypedValue.COMPLEX_UNIT_SP, value)
        }

    var detail: String?
        get() {
            return null
        }
        set(value) {
            detailLabel?.text = value?:""
        }
    var detailColor: Int
        get() {
            return 0
        }
        set(value) {
            detailLabel?.setTextColor(value)
        }
    var detailSize: Float
        get() {
            return 0F
        }
        set(value) {
            detailLabel?.setTextSize(TypedValue.COMPLEX_UNIT_SP, value)
        }

    var accessoryType: Int
        get() {
            return 0
        }
        set(value) {
            when (value) {
                ACCESSORY_TYPE_DEFAULT -> {

                }
                ACCESSORY_TYPE_ARROW -> {
                    accessoryLayout?.removeAllViews()
                    accessoryLayout?.addView(newArrowImageView())
                }
                ACCESSORY_TYPE_BADGE -> {
                    accessoryLayout?.removeAllViews()
                    accessoryLayout?.addView(newBadgeView())
                }
                ACCESSORY_TYPE_SWITCH -> {
                    accessoryLayout?.removeAllViews()
                    accessoryLayout?.addView(newSwitch())
                }
                ACCESSORY_TYPE_BUTTON -> {
                    accessoryLayout?.removeAllViews()
                    accessoryLayout?.addView(newButton())
                }
            }
        }

    init {
        val myView: View = LayoutInflater.from(context).inflate(R.layout.table_view_cell, null)
        imageView = myView.findViewById(R.id.imageView)
        titleLabel = myView.findViewById(R.id.titleLabel)
        detailLabel = myView.findViewById(R.id.detailLabel)
        accessoryLayout = myView.findViewById(R.id.accessoryLayout)
        lineView = myView.findViewById(R.id.lineView)

        addView(myView)

        if (attrs != null) {
            val ta: TypedArray = getContext().obtainStyledAttributes(attrs, R.styleable.UITableViewCell)
            image = ta.getResourceId(R.styleable.UITableViewCell_tvc_image, 0)
            title = ta.getString(R.styleable.UITableViewCell_tvc_title) ?: ""
            titleColor = ta.getColor(R.styleable.UITableViewCell_tvc_titleColor, ColorUtils.getColor(R.color.black))
            titleSize = ta.getDimension(R.styleable.UITableViewCell_tvc_titleSize, 17f)
            detail = ta.getString(R.styleable.UITableViewCell_tvc_detail) ?: ""
            detailColor = ta.getColor(R.styleable.UITableViewCell_tvc_detailColor, ColorUtils.getColor(R.color.black))
            detailSize = ta.getDimension(R.styleable.UITableViewCell_tvc_detailSize, 17f)
            accessoryType = ta.getInteger(R.styleable.UITableViewCell_tvc_accessory, 0)
        }

    }

    override fun setOnClickListener(l: OnClickListener?) {
        if (l != null) {
            setBackgroundResource(R.drawable.item_bg_white)
        } else {
            setBackgroundResource(0)
        }
        super.setOnClickListener(l)
    }

    private fun newArrowImageView() : ImageView {
        val imageView = ImageView(context)
        imageView.setImageResource(R.drawable.arrow_right)
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams.marginEnd = 10
        imageView.layoutParams = layoutParams
        return imageView
    }

    private fun newBadgeView() : BadgeView {
        badgeView = BadgeView(context)
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, SizeUtils.dp2px(20f))
        layoutParams.marginEnd = 10
        badgeView!!.layoutParams = layoutParams
        badgeView!!.visibility = GONE
        return badgeView!!
    }

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    private fun newSwitch() : Switch {
        switch = Switch(context)
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams.marginEnd = 10
        switch!!.layoutParams = layoutParams
        return switch!!
    }

    private fun newButton(): Button {
        button = Button(context)
        button!!.tag = "cell_button"
        val layoutParams = LinearLayout.LayoutParams(SizeUtils.dp2px(80f), SizeUtils.dp2px(40f))
        layoutParams.marginEnd = 10
        button!!.layoutParams = layoutParams
        button!!.setBackgroundColor(Color.RED)
        button!!.text = "删除"
        button!!.setTextColor(Color.WHITE)
        button!!.textSize = 13f
        return button!!
    }

}